﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.HVAC;
using YArchitech.MEP;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public class DrawVRefrigerantMgr
	{
		public void SetMark(Pipe vPipe, UIApplication uiApp, VPipeSetting config)
		{
			string text = string.Empty;
			UIDocument activeUIDocument = uiApp.ActiveUIDocument;
			using (CreateRiserPipeLinesJig createRiserPipeLinesJig = new CreateRiserPipeLinesJig(activeUIDocument))
			{
				XYZ vpipeCenter = this.GetVPipeCenter(vPipe, activeUIDocument);
				RiserPipeAndCenter riserPipeAndCenter = new RiserPipeAndCenter(vPipe, vpipeCenter);
				createRiserPipeLinesJig.StartPoint = vpipeCenter;
				XYZ xyz = null;
				XYZ subVector = null;
				XYZ ptLocation = activeUIDocument.Selection.PickPoint("请选择标注位置：");
				if (config.IsAreaNum)
				{
					text = text + config.AreaNum + "-";
				}
				if (config.IsBuildingNum)
				{
					text = text + config.BuildingNum + "-";
				}
				string customNum = config.CustomNum;
				MarkRiserPipeUtility.SetRiserPipeParameters(uiApp, riserPipeAndCenter.RiserPipe, text, customNum, config.IsCustom, false);
				double height = 0.0;
				double lineLength = this.getLineLength(activeUIDocument, riserPipeAndCenter, ref height);
				DetailCurveArray detailCurveArr = this.getDetailCurveArr(activeUIDocument, riserPipeAndCenter, lineLength, ref xyz, ptLocation, ref subVector, createRiserPipeLinesJig.isLeft);
				if (detailCurveArr != null)
				{
					Autodesk.Revit.DB.Group group = this.createGroup(detailCurveArr, activeUIDocument, riserPipeAndCenter, vPipe);
					IndependentTag independentTag = null;
					xyz = new XYZ(xyz.X, xyz.Y, activeUIDocument.ActiveView.GenLevel.Elevation);
					MarkRiserPipeUtility.MarkSinglePipeText(activeUIDocument.Document, activeUIDocument.Document.ActiveView, riserPipeAndCenter.RiserPipe, xyz, subVector, lineLength, config.IsAreaNum, config.IsBuildingNum, group, height, ref independentTag);
					activeUIDocument.Document.Regenerate();
				}
			}
		}

		private XYZ GetVPipeCenter(Pipe pipe, UIDocument UIDoc)
		{
			Line curve = (pipe.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = curve.GetEndPoint(0);
			curve.GetEndPoint(1);
			double elevation = UIDoc.ActiveView.GenLevel.Elevation;
			return new XYZ(hyendPoint.X, hyendPoint.Y, elevation);
		}

		private double getLineLength(UIDocument uidoc, RiserPipeAndCenter pipeAndCenter, ref double height)
		{
			string markFamilyPath = UnifiedModified.GetMarkFamilyPath(uidoc.Document, "HY_水管立管标记");
			uidoc.Document.LoadFamily(markFamilyPath);
			FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(uidoc.Document, "HY_水管立管标记", BuiltInCategory.OST_PipeTags);
			IndependentTag independentTag = RevitVersionFuncs.NewTag(uidoc.Document, uidoc.Document.ActiveView, new Reference(pipeAndCenter.RiserPipe), false, 0, 0, new XYZ(0.0, 0.0, 0.0));
			independentTag.ChangeTypeId(familySymbol.Id);
			MarkOperation markOperation = new MarkOperation();
			double num = 0.0;
			markOperation.GetTagTextWidthAndHeight(independentTag.TagText, uidoc.Document, ref num, ref height);
			double result = markOperation.GetCreatTagLength(independentTag.TagText, uidoc.Document);
			if (RevitVersion.GetVersionNumber(uidoc.Document) == "2015" || RevitVersion.GetVersionNumber(uidoc.Document) == "2014")
			{
				CommonFont fontSetting = new UserSetting().GetFontSetting();
				result = (double)independentTag.TagText.Length * height * fontSetting.FontGWRotia * 0.36;
			}
			height = markOperation.GetDistanceTagToLeader(uidoc.Document, independentTag.TagText) * 304.8;
			uidoc.Document.Delete(independentTag.Id);
			return result;
		}

		private DetailCurveArray getDetailCurveArr(UIDocument uidoc, RiserPipeAndCenter pipeAndCenter, double markingTextLength, ref XYZ textLocation, XYZ ptLocation, ref XYZ subLineDir, bool isleft)
		{
			List<Line> list = MarkRiserPipeUtility.DrawSinglePipeMarkLines(pipeAndCenter.Location, ptLocation, 0.0, false, Math.Abs(AssistFunc.feetToMM(ptLocation.Y - pipeAndCenter.Location.Y)), markingTextLength, ref textLocation, ref subLineDir, uidoc.Document, isleft);
			DetailCurveArray detailCurveArray = new DetailCurveArray();
			if (list.Count == 0)
			{
				return null;
			}
			foreach (Curve curve in list)
			{
				DetailCurve detailCurve = uidoc.Document.Create.NewDetailCurve(uidoc.ActiveView, curve);
				detailCurveArray.Append(detailCurve);
			}
			return detailCurveArray;
		}

		private Autodesk.Revit.DB.Group createGroup(DetailCurveArray detailCurveArr, UIDocument uidoc, RiserPipeAndCenter pipeAndCenter, Pipe RiserPipe)
		{
			List<RiserPipeAndCenter> list = new List<RiserPipeAndCenter>();
			list.Add(pipeAndCenter);
			Autodesk.Revit.DB.Group group = MarkRiserPipeUtility.WriteMarkGroupsToRiserPipe(uidoc.Document, list, detailCurveArr);
			AssociationMarkInfo.WriteMarkInfoToElement(group, YArchitech.HVAC.MarkType.RiserPipe);
			new AuxiliaryFrameOperation().SetAuxiliaryFramePipeGroup(uidoc.Document, RiserPipe, group);
			return group;
		}
	}
}
